!pip3 install plotly==4.14.1
import json
import plotly.figure_factory as ff
import plotly.graph_objects as go
import plotly.express as px
import plotly.offline as py
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
init_notebook_mode(connected=False)
import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)
import seaborn as sns
import matplotlib.pyplot as plt
import statsmodels.api as sm
import sklearn
import math
from datetime import datetime, date
from sklearn import preprocessing
from sklearn import datasets
from sklearn import utils
from sklearn import linear_model
from sklearn.metrics import *
from sklearn.preprocessing import *
from statsmodels.formula.api import ols
from sklearn.linear_model import LinearRegression
from sklearn.tree import DecisionTreeRegressor
from sklearn.neighbors import KNeighborsRegressor
from sklearn.model_selection import train_test_split
import pandas as pd
import numpy as np
facebook = pd.read_csv("data/Facebook.csv", sep=',')
apple = pd.read_csv("data/Apple.csv", sep=',')
amazon = pd.read_csv("data/Amazon.csv", sep=',')
netflix = pd.read_csv("data/Netflix.csv", sep=',')
google = pd.read_csv("data/Google.csv", sep=',')
facebook['Date'] = pd.to_datetime(facebook['Date'])
apple['Date'] = pd.to_datetime(apple['Date'])
amazon['Date'] = pd.to_datetime(amazon['Date'])
netflix['Date'] = pd.to_datetime(netflix['Date'])
google['Date'] = pd.to_datetime(google['Date'])
facebook = facebook[(facebook['Date'].dt.year > 2012) & (facebook['Date'].dt.year < 2021)]
apple = apple[(apple['Date'].dt.year > 2012) & (apple['Date'].dt.year < 2021)]
amazon = amazon[(amazon['Date'].dt.year > 2012) & (amazon['Date'].dt.year < 2021)]
netflix = netflix[(netflix['Date'].dt.year > 2012) & (netflix['Date'].dt.year < 2021)]
google = google[(google['Date'].dt.year > 2012) & (google['Date'].dt.year < 2021)]
facebook = facebook.reset_index(drop=True)
apple = apple.reset_index(drop=True)
amazon = amazon.reset_index(drop=True)
netflix = netflix.reset_index(drop=True)
google = google.reset_index(drop=True)
facebook
df_corr = pd.DataFrame()
df_corr['Facebook'] = facebook['Close']
df_corr['Apple'] = apple['Close']
df_corr['Amazon'] = amazon['Close']
df_corr['Netflix'] = netflix['Close']
df_corr['Google'] = google['Close']
retscomp = df_corr.pct_change()
corr = retscomp.corr()
corr
fig = px.imshow(corr)
iplot(fig,show_link=False)
corr_df_fb = facebook[['Open', 'Close', 'High', 'Low', 'Adj Close', 'Volume']].copy(deep=True)
retscomp_fb = corr_df_fb.pct_change()
corr_fb = retscomp_fb.corr()
corr_fb
facebook['Company'] = ['Facebook']*len(facebook)
apple['Company'] = ['Apple']*len(apple)
amazon['Company'] = ['Amazon']*len(amazon)
netflix['Company'] = ['Netflix']*len(netflix)
google['Company'] = ['Google']*len(google)
frames = [facebook, apple, amazon, netflix, google]
result = pd.concat(frames)
fig = go.Figure()
fig.add_trace(go.Scatter(x=facebook.Date, y=facebook.Close, name='FB'))
fig.add_trace(go.Scatter(x=apple.Date, y=apple.Close, name='AAPL'))
fig.add_trace(go.Scatter(x=amazon.Date, y=amazon.Close, name='AMZN'))
fig.add_trace(go.Scatter(x=netflix.Date, y=netflix.Close, name='NFLX'))
fig.add_trace(go.Scatter(x=google.Date, y=google.Close, name='GOOG'))
fig.update_layout(title='Close prices for All Companies from Jan 2013 to Aug 2020',
xaxis_title='Date',
yaxis_title='Close Price')
fig.update_layout(
updatemenus=[
dict(
buttons=list([
dict(label = 'All',
method = 'update',
args = [{'visible': [True, True, True, True, True]},
{'title': 'All',
'showlegend':True}]),
dict(label = 'Facebook',
method = 'update',
args = [{'visible': [True, False, False, False, False]},
{'title': 'FB',
'showlegend':True}]),
dict(label = 'Apple',
method = 'update',
args = [{'visible': [False, True, False, False, False]},
{'title': 'APPL',
'showlegend':True}]),
dict(label = 'Amazon',
method = 'update',
args = [{'visible': [False, False, True, False, False]},
{'title': 'AMZN',
'showlegend':True}]),
dict(label = 'Netflix',
method = 'update',
args = [{'visible': [False, False, False, True, False]},
{'title': 'NFLX',
'showlegend':True}]),
dict(label = 'Google',
method = 'update',
args = [{'visible': [False, False, False, False, True]},
{'title': 'GOOG',
'showlegend':True}]),
]),
direction="down",
pad={"r": 10, "t": 10},
showactive=True,
x=0.1,
xanchor="left",
y=1.1,
yanchor="top"
),
]
)
fig.update_layout(
autosize=False,
width=1000,
height=650,)
iplot(fig,show_link=False)
result['Year'] = np.arange(len(result.index))
result['Date'] = pd.to_datetime(result['Date'])
for x, rows in result.iterrows():
result.loc[x, 'Year'] = rows['Date'].year
comp = result.groupby(['Company', 'Year'])
vol_df = pd.DataFrame()
vol = []
company = []
year = []
x = 0
for key,val in comp:
a,b = key
company.append(a)
year.append(b)
vol.append(comp.get_group(key).mean()['Volume'])
vol_df['Company'] = company
vol_df['Year'] = year
vol_df['Volume Mean'] = vol
fig = go.Figure()
avg_vol = vol_df['Volume Mean'].mean()
stand_vol = vol_df['Volume Mean'].std()
vol_df['standard_vol'] = np.arange(len(vol_df.index))
vol_df = vol_df.reset_index(drop=True)
for x, rows in vol_df.iterrows():
vol_df.loc[x, 'standard_vol'] = (rows['Volume Mean'] - avg_vol)/(stand_vol)
fig.add_trace(go.Bar(x=vol_df[vol_df['Company'] == 'Facebook']['Year'], y=vol_df[vol_df['Company'] == 'Facebook']['standard_vol'], name='FB'))
fig.add_trace(go.Bar(x=vol_df[vol_df['Company'] == 'Apple']['Year'], y=vol_df[vol_df['Company'] == 'Apple']['standard_vol'], name='AAPL'))
fig.add_trace(go.Bar(x=vol_df[vol_df['Company'] == 'Amazon']['Year'], y=vol_df[vol_df['Company'] == 'Amazon']['standard_vol'], name='AMZN'))
fig.add_trace(go.Bar(x=vol_df[vol_df['Company'] == 'Netflix']['Year'], y=vol_df[vol_df['Company'] == 'Netflix']['standard_vol'], name='NFLX'))
fig.add_trace(go.Bar(x=vol_df[vol_df['Company'] == 'Google']['Year'], y=vol_df[vol_df['Company'] == 'Google']['standard_vol'], name='GOOG'))
fig.update_layout(title='Standardized Volume for All Companies from Jan 2013 to Aug 2020 Grouped by Year',
xaxis_title='Date',
yaxis_title='Standard Volume')
iplot(fig,show_link=False)
avg_14 = facebook.Close.rolling(window=14, min_periods=1).mean()
avg_21 = facebook.Close.rolling(window=21, min_periods=1).mean()
avg_100 = facebook.Close.rolling(window=100, min_periods=1).mean()
x_fb = facebook['Date']
y_fb = facebook['Open']
z_fb = facebook['Close']
fig = go.Figure()
fig.add_trace(go.Scatter(x=x_fb, y=y_fb, name='Open',
line=dict(color='royalblue', width=1.5)))
fig.add_trace(go.Scatter(x=x_fb, y=z_fb, name = 'Close',
line=dict(color='firebrick', width=1.5)))
fig.add_trace(go.Scatter(x=x_fb, y=avg_14, name = '14 Day Close Avg',
line=dict(color='gold', width=1.5)))
fig.add_trace(go.Scatter(x=x_fb, y=avg_21, name = '21 Day Close Avg',
line=dict(color='orangered', width=1.5)))
fig.add_trace(go.Scatter(x=x_fb, y=avg_100, name = '100 Day Close Avg',
line=dict(color='mediumorchid', width=1.5)))
fig.add_trace(go.Bar(x=vol_df[vol_df['Company'] == 'Facebook']['Year'],
y=vol_df[vol_df['Company'] == 'Facebook']['Volume Mean']/200000, name='Volume (scaled)',
marker_color='slategray', opacity=0.3))
fig.add_trace(go.Bar(x=vol_df[vol_df['Company'] == 'Facebook']['Year'],
y=vol_df[vol_df['Company'] == 'Facebook']['Volume Mean'], name='Volume',
marker_color='slategray', visible='legendonly'))
fig.update_layout(title='Open/Close prices and Volume for Facebook from Jan 2013 to Aug 2020',
xaxis_title='Date',
yaxis_title='Open/Close/Volume')
fig.update_layout(
updatemenus=[
dict(
buttons=list([
dict(label = 'All',
method = 'update',
args = [{'visible': [True, True, True, True, True, True, False]},
{'title': 'All',
'showlegend':True}]),
dict(label = 'Open Price',
method = 'update',
args = [{'visible': [True, False, False, False, False, False, False]},
{'title': 'Open Price',
'showlegend':True}]),
dict(label = 'Close Price',
method = 'update',
args = [{'visible': [False, True, False, False, False, False, False]},
{'title': 'Close Price',
'showlegend':True}]),
dict(label = '14 Day Moving Average',
method = 'update',
args = [{'visible': [False, False, True, False, False, False, False]},
{'title': '14 Day Moving Average',
'showlegend':True}]),
dict(label = '21 Day Moving Average',
method = 'update',
args = [{'visible': [False, False, False, True, False, False, False]},
{'title': '21 Day Moving Average',
'showlegend':True}]),
dict(label = '100 Day Moving Average',
method = 'update',
args = [{'visible': [False, False, False, False, True, False, False]},
{'title': '100 Day Moving Average',
'showlegend':True}]),
dict(label = 'Volume (not scaled)',
method = 'update',
args = [{'visible': [False, False, False, False, False, False, True]},
{'title': '100 Day Moving Average',
'showlegend':True}]),
]),
direction="down",
pad={"r": 10, "t": 10},
showactive=True,
x=0.1,
xanchor="left",
y=1.1,
yanchor="top"
),
]
)
fig.update_layout(
autosize=False,
width=1000,
height=650,)
iplot(fig,show_link=False)
avg_14 = apple.Close.rolling(window=14, min_periods=1).mean()
avg_21 = apple.Close.rolling(window=21, min_periods=1).mean()
avg_100 = apple.Close.rolling(window=100, min_periods=1).mean()
x_ap = apple['Date']
y_ap = apple['Open']
z_ap = apple['Close']
fig = go.Figure()
fig.add_trace(go.Scatter(x=x_ap, y=y_ap, name='Open',
line=dict(color='royalblue', width=1.5)))
fig.add_trace(go.Scatter(x=x_ap, y=z_ap, name = 'Close',
line=dict(color='firebrick', width=1.5)))
fig.add_trace(go.Scatter(x=x_fb, y=avg_14, name = '14 Day Close Avg',
line=dict(color='gold', width=1.5)))
fig.add_trace(go.Scatter(x=x_fb, y=avg_21, name = '21 Day Close Avg',
line=dict(color='orangered', width=1.5)))
fig.add_trace(go.Scatter(x=x_fb, y=avg_100, name = '100 Day Close Avg',
line=dict(color='mediumorchid', width=1.5)))
fig.add_trace(go.Bar(x=vol_df[vol_df['Company'] == 'Apple']['Year'],
y=vol_df[vol_df['Company'] == 'Apple']['Volume Mean']/3500000, name='Volume (scaled)',
marker_color='slategray', opacity=0.3))
fig.add_trace(go.Bar(x=vol_df[vol_df['Company'] == 'Apple']['Year'],
y=vol_df[vol_df['Company'] == 'Apple']['Volume Mean'], name='Volume',
marker_color='slategray', visible='legendonly'))
fig.update_layout(title='Open/Close prices and Volume for Apple from Jan 2013 to Aug 2020',
xaxis_title='Date',
yaxis_title='Open/Close/Volume')
fig.update_layout(
updatemenus=[
dict(
buttons=list([
dict(label = 'All',
method = 'update',
args = [{'visible': [True, True, True, True, True, True, False]},
{'title': 'All',
'showlegend':True}]),
dict(label = 'Open Price',
method = 'update',
args = [{'visible': [True, False, False, False, False, False, False]},
{'title': 'Open Price',
'showlegend':True}]),
dict(label = 'Close Price',
method = 'update',
args = [{'visible': [False, True, False, False, False, False, False]},
{'title': 'Close Price',
'showlegend':True}]),
dict(label = '14 Day Moving Average',
method = 'update',
args = [{'visible': [False, False, True, False, False, False, False]},
{'title': '14 Day Moving Average',
'showlegend':True}]),
dict(label = '21 Day Moving Average',
method = 'update',
args = [{'visible': [False, False, False, True, False, False, False]},
{'title': '21 Day Moving Average',
'showlegend':True}]),
dict(label = '100 Day Moving Average',
method = 'update',
args = [{'visible': [False, False, False, False, True, False, False]},
{'title': '100 Day Moving Average',
'showlegend':True}]),
dict(label = 'Volume (not scaled)',
method = 'update',
args = [{'visible': [False, False, False, False, False, False, True]},
{'title': 'Volume (not scaled)',
'showlegend':True}]),
]),
direction="down",
pad={"r": 10, "t": 10},
showactive=True,
x=0.1,
xanchor="left",
y=1.1,
yanchor="top"
),
]
)
fig.update_layout(
autosize=False,
width=1000,
height=650,)
iplot(fig,show_link=False)
avg_14 = amazon.Close.rolling(window=14, min_periods=1).mean()
avg_21 = amazon.Close.rolling(window=21, min_periods=1).mean()
avg_100 = amazon.Close.rolling(window=100, min_periods=1).mean()
x_am = amazon['Date']
y_am = amazon['Open']
z_am = amazon['Close']
fig = go.Figure()
fig.add_trace(go.Scatter(x=x_am, y=y_am, name='Open',
line=dict(color='royalblue', width=1.5)))
fig.add_trace(go.Scatter(x=x_am, y=z_am, name = 'Close',
line=dict(color='firebrick', width=1.5)))
fig.add_trace(go.Scatter(x=x_fb, y=avg_14, name = '14 Day Close Avg',
line=dict(color='gold', width=1.5)))
fig.add_trace(go.Scatter(x=x_fb, y=avg_21, name = '21 Day Close Avg',
line=dict(color='orangered', width=1.5)))
fig.add_trace(go.Scatter(x=x_fb, y=avg_100, name = '100 Day Close Avg',
line=dict(color='mediumorchid', width=1.5)))
fig.add_trace(go.Bar(x=vol_df[vol_df['Company'] == 'Amazon']['Year'],
y=vol_df[vol_df['Company'] == 'Amazon']['Volume Mean']/2000, name='Volume (scaled)',
marker_color='slategray', opacity=0.3))
fig.add_trace(go.Bar(x=vol_df[vol_df['Company'] == 'Amazon']['Year'],
y=vol_df[vol_df['Company'] == 'Amazon']['Volume Mean'], name='Volume',
marker_color='slategray', visible='legendonly'))
fig.update_layout(title='Open/Close prices and Volume for Amazon from Jan 2013 to Aug 2020',
xaxis_title='Date',
yaxis_title='Open/Close/Volume')
fig.update_layout(
updatemenus=[
dict(
buttons=list([
dict(label = 'All',
method = 'update',
args = [{'visible': [True, True, True, True, True, True, False]},
{'title': 'All',
'showlegend':True}]),
dict(label = 'Open Price',
method = 'update',
args = [{'visible': [True, False, False, False, False, False, False]},
{'title': 'Open Price',
'showlegend':True}]),
dict(label = 'Close Price',
method = 'update',
args = [{'visible': [False, True, False, False, False, False, False]},
{'title': 'Close Price',
'showlegend':True}]),
dict(label = '14 Day Moving Average',
method = 'update',
args = [{'visible': [False, False, True, False, False, False, False]},
{'title': '14 Day Moving Average',
'showlegend':True}]),
dict(label = '21 Day Moving Average',
method = 'update',
args = [{'visible': [False, False, False, True, False, False, False]},
{'title': '21 Day Moving Average',
'showlegend':True}]),
dict(label = '100 Day Moving Average',
method = 'update',
args = [{'visible': [False, False, False, False, True, False, False]},
{'title': '100 Day Moving Average',
'showlegend':True}]),
dict(label = 'Volume (not scaled)',
method = 'update',
args = [{'visible': [False, False, False, False, False, False, True]},
{'title': 'Volume (not scaled)',
'showlegend':True}]),
]),
direction="down",
pad={"r": 10, "t": 10},
showactive=True,
x=0.1,
xanchor="left",
y=1.1,
yanchor="top"
),
]
)
fig.update_layout(
autosize=False,
width=1000,
height=650,)
iplot(fig,show_link=False)
avg_14 = netflix.Close.rolling(window=14, min_periods=1).mean()
avg_21 = netflix.Close.rolling(window=21, min_periods=1).mean()
avg_100 = netflix.Close.rolling(window=100, min_periods=1).mean()
x_ne = netflix['Date']
y_ne = netflix['Open']
z_ne = netflix['Close']
fig = go.Figure()
fig.add_trace(go.Scatter(x=x_ne, y=y_ne, name='Open',
line=dict(color='royalblue', width=1.5)))
fig.add_trace(go.Scatter(x=x_ne, y=z_ne, name = 'Close',
line=dict(color='firebrick', width=1.5)))
fig.add_trace(go.Scatter(x=x_fb, y=avg_14, name = '14 Day Close Avg',
line=dict(color='gold', width=1.5)))
fig.add_trace(go.Scatter(x=x_fb, y=avg_21, name = '21 Day Close Avg',
line=dict(color='orangered', width=1.5)))
fig.add_trace(go.Scatter(x=x_fb, y=avg_100, name = '100 Day Close Avg',
line=dict(color='mediumorchid', width=1.5)))
fig.add_trace(go.Bar(x=vol_df[vol_df['Company'] == 'Netflix']['Year'],
y=vol_df[vol_df['Company'] == 'Netflix']['Volume Mean']/50000, name='Volume (scaled)',
marker_color='slategray', opacity=0.3))
fig.add_trace(go.Bar(x=vol_df[vol_df['Company'] == 'Netflix']['Year'],
y=vol_df[vol_df['Company'] == 'Netflix']['Volume Mean'], name='Volume',
marker_color='slategray', visible='legendonly'))
fig.update_layout(title='Open/Close prices and Volume for Netflix from Jan 2013 to Aug 2020',
xaxis_title='Date',
yaxis_title='Open/Close/Volume')
fig.update_layout(
updatemenus=[
dict(
buttons=list([
dict(label = 'All',
method = 'update',
args = [{'visible': [True, True, True, True, True, True, False]},
{'title': 'All',
'showlegend':True}]),
dict(label = 'Open Price',
method = 'update',
args = [{'visible': [True, False, False, False, False, False, False]},
{'title': 'Open Price',
'showlegend':True}]),
dict(label = 'Close Price',
method = 'update',
args = [{'visible': [False, True, False, False, False, False, False]},
{'title': 'Close Price',
'showlegend':True}]),
dict(label = '14 Day Moving Average',
method = 'update',
args = [{'visible': [False, False, True, False, False, False, False]},
{'title': '14 Day Moving Average',
'showlegend':True}]),
dict(label = '21 Day Moving Average',
method = 'update',
args = [{'visible': [False, False, False, True, False, False, False]},
{'title': '21 Day Moving Average',
'showlegend':True}]),
dict(label = '100 Day Moving Average',
method = 'update',
args = [{'visible': [False, False, False, False, True, False, False]},
{'title': '100 Day Moving Average',
'showlegend':True}]),
dict(label = 'Volume (not scaled)',
method = 'update',
args = [{'visible': [False, False, False, False, False, False, True]},
{'title': 'Volume (not scaled)',
'showlegend':True}]),
]),
direction="down",
pad={"r": 10, "t": 10},
showactive=True,
x=0.1,
xanchor="left",
y=1.1,
yanchor="top"
),
]
)
fig.update_layout(
autosize=False,
width=1000,
height=650,)
iplot(fig,show_link=False)
avg_14 = google.Close.rolling(window=14, min_periods=1).mean()
avg_21 = google.Close.rolling(window=21, min_periods=1).mean()
avg_100 = google.Close.rolling(window=100, min_periods=1).mean()
x_go = google['Date']
y_go = google['Open']
z_go = google['Close']
fig = go.Figure()
fig.add_trace(go.Scatter(x=x_go, y=y_go, name='Open',
line=dict(color='royalblue', width=1.5)))
fig.add_trace(go.Scatter(x=x_go, y=z_go, name = 'Close',
line=dict(color='firebrick', width=1.5)))
fig.add_trace(go.Scatter(x=x_fb, y=avg_14, name = '14 Day Close Avg',
line=dict(color='gold', width=1.5)))
fig.add_trace(go.Scatter(x=x_fb, y=avg_21, name = '21 Day Close Avg',
line=dict(color='orangered', width=1.5)))
fig.add_trace(go.Scatter(x=x_fb, y=avg_100, name = '100 Day Close Avg',
line=dict(color='mediumorchid', width=1.5)))
fig.add_trace(go.Bar(x=vol_df[vol_df['Company'] == 'Google']['Year'],
y=vol_df[vol_df['Company'] == 'Google']['Volume Mean']/2000, name='Volume (scaled)',
marker_color='slategray', opacity=0.3))
fig.add_trace(go.Bar(x=vol_df[vol_df['Company'] == 'Google']['Year'],
y=vol_df[vol_df['Company'] == 'Google']['Volume Mean'], name='Volume',
marker_color='slategray', visible='legendonly'))
fig.update_layout(title='Open/Close prices and Volume for Google from Jan 2013 to Aug 2020',
xaxis_title='Date',
yaxis_title='Open/Close/Volume')
fig.update_layout(
updatemenus=[
dict(
buttons=list([
dict(label = 'All',
method = 'update',
args = [{'visible': [True, True, True, True, True, True, False]},
{'title': 'All',
'showlegend':True}]),
dict(label = 'Open Price',
method = 'update',
args = [{'visible': [True, False, False, False, False, False, False]},
{'title': 'Open Price',
'showlegend':True}]),
dict(label = 'Close Price',
method = 'update',
args = [{'visible': [False, True, False, False, False, False, False]},
{'title': 'Close Price',
'showlegend':True}]),
dict(label = '14 Day Moving Average',
method = 'update',
args = [{'visible': [False, False, True, False, False, False, False]},
{'title': '14 Day Moving Average',
'showlegend':True}]),
dict(label = '21 Day Moving Average',
method = 'update',
args = [{'visible': [False, False, False, True, False, False, False]},
{'title': '21 Day Moving Average',
'showlegend':True}]),
dict(label = '100 Day Moving Average',
method = 'update',
args = [{'visible': [False, False, False, False, True, False, False]},
{'title': '100 Day Moving Average',
'showlegend':True}]),
dict(label = 'Volume (not scaled)',
method = 'update',
args = [{'visible': [False, False, False, False, False, False, True]},
{'title': 'Volume (not scaled)',
'showlegend':True}]),
]),
direction="down",
pad={"r": 10, "t": 10},
showactive=True,
x=0.1,
xanchor="left",
y=1.1,
yanchor="top"
),
]
)
fig.update_layout(
autosize=False,
width=1000,
height=650,)
iplot(fig,show_link=False)
avg_close = result.groupby('Date')['Close'].mean()
stand_close = result.groupby('Date')['Close'].std()
stand_close = stand_close.reset_index()
avg_close = avg_close.reset_index()
result['standard_close'] = np.arange(len(result.index))
result = result.reset_index(drop=True)
for x, rows in result.iterrows():
result.loc[x, 'standard_close'] = (rows['Close'] - avg_close[avg_close['Date'] == rows['Date']]['Close']).values/(stand_close[stand_close['Date'] == rows['Date']]['Close']).values
result
fig = px.line(result, x="Date", y="standard_close", color='Company')
fig.update_layout(title='Standardized Close prices for All Companies from Jan 2013 to Aug 2020',
xaxis_title='Date',
yaxis_title='Standardized Close Price')
iplot(fig,show_link=False)
facebook['timestamp'] = pd.to_datetime(facebook.Date).astype(int) // (10**9)
X = np.array(facebook['timestamp']).reshape(-1,1)
y = np.array(facebook['Close'])
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
model = LinearRegression()
model.fit(X_train, y_train)
x_range = np.linspace(X.min(), X.max(), 100)
y_range = model.predict(x_range.reshape(-1, 1))
fig = go.Figure()
fig.add_trace(go.Scatter(x=X_train.squeeze(), y=y_train, name='Training Data', mode='markers'))
fig.add_trace(go.Scatter(x=X_test.squeeze(), y=y_test, name='Testing Data', mode='markers'))
fig.add_trace(go.Scatter(x=x_range, y=y_range, name='Linear Regression'))
model = KNeighborsRegressor()
model.fit(X_train, y_train)
x_range = np.linspace(X.min(), X.max(), 100)
y_range = model.predict(x_range.reshape(-1, 1))
fig.add_trace(go.Scatter(x=x_range, y=y_range, name='kNN Regressor'))
model = DecisionTreeRegressor()
model.fit(X_train, y_train)
x_range = np.linspace(X.min(), X.max(), 100)
y_range = model.predict(x_range.reshape(-1, 1))
fig.add_trace(go.Scatter(x=x_range, y=y_range, name='Decision Tree'))
fig.update_layout(
updatemenus=[
dict(
buttons=list([
dict(label = 'All',
method = 'update',
args = [{'visible': [True, True, True, True, True]},
{'title': 'All',
'showlegend':True}]),
dict(label = 'Linear Regression',
method = 'update',
args = [{'visible': [True, True, True, False, False]},
{'title': 'Linear Regression',
'showlegend':True}]),
dict(label = 'k-NN Regressor',
method = 'update',
args = [{'visible': [True, True, False, True, False]},
{'title': 'k-NN Regressor',
'showlegend':True}]),
dict(label = 'Decision Tree Regressor',
method = 'update',
args = [{'visible': [True, True, False, False, True]},
{'title': 'Decision Tree Regressor',
'showlegend':True}]),
]),
direction="down",
pad={"r": 10, "t": 10},
showactive=True,
x=0.1,
xanchor="left",
y=1.1,
yanchor="top"
),
]
)
fig.update_layout(title='Regression Line Fit for Facebook from Jan 2013 to Aug 2020',
xaxis_title='Date',
yaxis_title='Close Price')
fig.update_layout(
autosize=False,
width=1000,
height=650,)
iplot(fig,show_link=False)
apple['timestamp'] = pd.to_datetime(apple.Date).astype(int) // (10**9)
X = np.array(apple['timestamp']).reshape(-1,1)
y = np.array(apple['Close'])
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
model = LinearRegression()
model.fit(X_train, y_train)
x_range = np.linspace(X.min(), X.max(), 100)
y_range = model.predict(x_range.reshape(-1, 1))
fig = go.Figure()
fig.add_trace(go.Scatter(x=X_train.squeeze(), y=y_train, name='Training Data', mode='markers'))
fig.add_trace(go.Scatter(x=X_test.squeeze(), y=y_test, name='Testing Data', mode='markers'))
fig.add_trace(go.Scatter(x=x_range, y=y_range, name='Linear Regression'))
model = KNeighborsRegressor()
model.fit(X_train, y_train)
x_range = np.linspace(X.min(), X.max(), 100)
y_range = model.predict(x_range.reshape(-1, 1))
fig.add_trace(go.Scatter(x=x_range, y=y_range, name='kNN Regressor'))
model = DecisionTreeRegressor()
model.fit(X_train, y_train)
x_range = np.linspace(X.min(), X.max(), 100)
y_range = model.predict(x_range.reshape(-1, 1))
fig.add_trace(go.Scatter(x=x_range, y=y_range, name='Decision Tree'))
fig.update_layout(
updatemenus=[
dict(
buttons=list([
dict(label = 'All',
method = 'update',
args = [{'visible': [True, True, True, True, True]},
{'title': 'All',
'showlegend':True}]),
dict(label = 'Linear Regression',
method = 'update',
args = [{'visible': [True, True, True, False, False]},
{'title': 'Linear Regression',
'showlegend':True}]),
dict(label = 'k-NN Regressor',
method = 'update',
args = [{'visible': [True, True, False, True, False]},
{'title': 'k-NN Regressor',
'showlegend':True}]),
dict(label = 'Decision Tree Regressor',
method = 'update',
args = [{'visible': [True, True, False, False, True]},
{'title': 'Decision Tree Regressor',
'showlegend':True}]),
]),
direction="down",
pad={"r": 10, "t": 10},
showactive=True,
x=0.1,
xanchor="left",
y=1.1,
yanchor="top"
),
]
)
fig.update_layout(title='Regression Line Fit for Apple from Jan 2013 to Aug 2020',
xaxis_title='Date',
yaxis_title='Close Price')
fig.update_layout(
autosize=False,
width=1000,
height=650,)
iplot(fig,show_link=False)
amazon['timestamp'] = pd.to_datetime(amazon.Date).astype(int) // (10**9)
X = np.array(amazon['timestamp']).reshape(-1,1)
y = np.array(amazon['Close'])
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
model = LinearRegression()
model.fit(X_train, y_train)
x_range = np.linspace(X.min(), X.max(), 100)
y_range = model.predict(x_range.reshape(-1, 1))
fig = go.Figure()
fig.add_trace(go.Scatter(x=X_train.squeeze(), y=y_train, name='Training Data', mode='markers'))
fig.add_trace(go.Scatter(x=X_test.squeeze(), y=y_test, name='Testing Data', mode='markers'))
fig.add_trace(go.Scatter(x=x_range, y=y_range, name='Linear Regression'))
model = KNeighborsRegressor()
model.fit(X_train, y_train)
x_range = np.linspace(X.min(), X.max(), 100)
y_range = model.predict(x_range.reshape(-1, 1))
fig.add_trace(go.Scatter(x=x_range, y=y_range, name='kNN Regressor'))
model = DecisionTreeRegressor()
model.fit(X_train, y_train)
x_range = np.linspace(X.min(), X.max(), 100)
y_range = model.predict(x_range.reshape(-1, 1))
fig.add_trace(go.Scatter(x=x_range, y=y_range, name='Decision Tree'))
fig.update_layout(
updatemenus=[
dict(
buttons=list([
dict(label = 'All',
method = 'update',
args = [{'visible': [True, True, True, True, True]},
{'title': 'All',
'showlegend':True}]),
dict(label = 'Linear Regression',
method = 'update',
args = [{'visible': [True, True, True, False, False]},
{'title': 'Linear Regression',
'showlegend':True}]),
dict(label = 'k-NN Regressor',
method = 'update',
args = [{'visible': [True, True, False, True, False]},
{'title': 'k-NN Regressor',
'showlegend':True}]),
dict(label = 'Decision Tree Regressor',
method = 'update',
args = [{'visible': [True, True, False, False, True]},
{'title': 'Decision Tree Regressor',
'showlegend':True}]),
]),
direction="down",
pad={"r": 10, "t": 10},
showactive=True,
x=0.1,
xanchor="left",
y=1.1,
yanchor="top"
),
]
)
fig.update_layout(title='Regression Line Fit for Amazon from Jan 2013 to Aug 2020',
xaxis_title='Date',
yaxis_title='Close Price')
fig.update_layout(
autosize=False,
width=1000,
height=650,)
iplot(fig,show_link=False)
netflix['timestamp'] = pd.to_datetime(netflix.Date).astype(int) // (10**9)
X = np.array(netflix['timestamp']).reshape(-1,1)
y = np.array(netflix['Close'])
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
model = LinearRegression()
model.fit(X_train, y_train)
x_range = np.linspace(X.min(), X.max(), 100)
y_range = model.predict(x_range.reshape(-1, 1))
fig = go.Figure()
fig.add_trace(go.Scatter(x=X_train.squeeze(), y=y_train, name='Training Data', mode='markers'))
fig.add_trace(go.Scatter(x=X_test.squeeze(), y=y_test, name='Testing Data', mode='markers'))
fig.add_trace(go.Scatter(x=x_range, y=y_range, name='Linear Regression'))
model = KNeighborsRegressor()
model.fit(X_train, y_train)
x_range = np.linspace(X.min(), X.max(), 100)
y_range = model.predict(x_range.reshape(-1, 1))
fig.add_trace(go.Scatter(x=x_range, y=y_range, name='kNN Regressor'))
model = DecisionTreeRegressor()
model.fit(X_train, y_train)
x_range = np.linspace(X.min(), X.max(), 100)
y_range = model.predict(x_range.reshape(-1, 1))
fig.add_trace(go.Scatter(x=x_range, y=y_range, name='Decision Tree'))
fig.update_layout(
updatemenus=[
dict(
buttons=list([
dict(label = 'All',
method = 'update',
args = [{'visible': [True, True, True, True, True]},
{'title': 'All',
'showlegend':True}]),
dict(label = 'Linear Regression',
method = 'update',
args = [{'visible': [True, True, True, False, False]},
{'title': 'Linear Regression',
'showlegend':True}]),
dict(label = 'k-NN Regressor',
method = 'update',
args = [{'visible': [True, True, False, True, False]},
{'title': 'k-NN Regressor',
'showlegend':True}]),
dict(label = 'Decision Tree Regressor',
method = 'update',
args = [{'visible': [True, True, False, False, True]},
{'title': 'Decision Tree Regressor',
'showlegend':True}]),
]),
direction="down",
pad={"r": 10, "t": 10},
showactive=True,
x=0.1,
xanchor="left",
y=1.1,
yanchor="top"
),
]
)
fig.update_layout(title='Regression Line Fit for Netflix from Jan 2013 to Aug 2020',
xaxis_title='Date',
yaxis_title='Close Price')
fig.update_layout(
autosize=False,
width=1000,
height=650,)
iplot(fig,show_link=False)
google['timestamp'] = pd.to_datetime(google.Date).astype(int) // (10**9)
X = np.array(google['timestamp']).reshape(-1,1)
y = np.array(google['Close'])
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
model = LinearRegression()
model.fit(X_train, y_train)
x_range = np.linspace(X.min(), X.max(), 100)
y_range = model.predict(x_range.reshape(-1, 1))
fig = go.Figure()
fig.add_trace(go.Scatter(x=X_train.squeeze(), y=y_train, name='Training Data', mode='markers'))
fig.add_trace(go.Scatter(x=X_test.squeeze(), y=y_test, name='Testing Data', mode='markers'))
fig.add_trace(go.Scatter(x=x_range, y=y_range, name='Linear Regression'))
model = KNeighborsRegressor()
model.fit(X_train, y_train)
x_range = np.linspace(X.min(), X.max(), 100)
y_range = model.predict(x_range.reshape(-1, 1))
fig.add_trace(go.Scatter(x=x_range, y=y_range, name='kNN Regressor'))
model = DecisionTreeRegressor()
model.fit(X_train, y_train)
x_range = np.linspace(X.min(), X.max(), 100)
y_range = model.predict(x_range.reshape(-1, 1))
fig.add_trace(go.Scatter(x=x_range, y=y_range, name='Decision Tree'))
fig.update_layout(
updatemenus=[
dict(
buttons=list([
dict(label = 'All',
method = 'update',
args = [{'visible': [True, True, True, True, True]},
{'title': 'All',
'showlegend':True}]),
dict(label = 'Linear Regression',
method = 'update',
args = [{'visible': [True, True, True, False, False]},
{'title': 'Linear Regression',
'showlegend':True}]),
dict(label = 'k-NN Regressor',
method = 'update',
args = [{'visible': [True, True, False, True, False]},
{'title': 'k-NN Regressor',
'showlegend':True}]),
dict(label = 'Decision Tree Regressor',
method = 'update',
args = [{'visible': [True, True, False, False, True]},
{'title': 'Decision Tree Regressor',
'showlegend':True}]),
]),
direction="down",
pad={"r": 10, "t": 10},
showactive=True,
x=0.1,
xanchor="left",
y=1.1,
yanchor="top"
),
]
)
fig.update_layout(title='Regression Line Fit for Google from Jan 2013 to Aug 2020',
xaxis_title='Date',
yaxis_title='Close Price')
fig.update_layout(
autosize=False,
width=1000,
height=650,)
iplot(fig,show_link=False)
df = facebook[['Close']].copy(deep=True)
future_days = 500
df['Prediction'] = df[['Close']].shift(-future_days)
X = np.array(df.drop(['Prediction'], 1))[:-future_days]
y = np.array(df['Prediction'])[:-future_days]
x_train, x_test, y_train, y_test = train_test_split(X, y, test_size = 0.25)
tree = DecisionTreeRegressor().fit(x_train, y_train)
lr = LinearRegression().fit(x_train, y_train)
knn = KNeighborsRegressor().fit(x_train, y_train)
x_future = df.drop(['Prediction'], 1)[:-future_days]
x_future = x_future.tail(future_days)
x_future = np.array(x_future)
tree_prediction = tree.predict(x_future)
lr_prediction = lr.predict(x_future)
knn_prediction = knn.predict(x_future)
predictions = tree_prediction
valid = df[X.shape[0]:].copy(deep=True)
valid['Predictions'] = predictions
valid[['Close','Predictions']]
fig = go.Figure()
fig.add_trace(go.Scatter(x=df.index.values, y=df['Close'], name='Actual Close',
line=dict(width=1.5)))
fig.add_trace(go.Scatter(x=valid.index.values, y=valid['Predictions'], name='Predicted Close D-Tree',
line=dict(width=1.5)))
predictions = lr_prediction
valid = df[X.shape[0]:].copy(deep=True)
valid['Predictions'] = predictions
valid[['Close','Predictions']]
fig.add_trace(go.Scatter(x=valid.index.values, y=valid['Predictions'], name='Predicted Close Lin Reg',
line=dict(width=1.5)))
predictions = knn_prediction
valid = df[X.shape[0]:].copy(deep=True)
valid['Predictions'] = predictions
valid[['Close','Predictions']]
fig.add_trace(go.Scatter(x=valid.index.values, y=valid['Predictions'], name='Predicted Close k-NN',
line=dict(width=1.5)))
fig.update_layout(
updatemenus=[
dict(
buttons=list([
dict(label = 'All',
method = 'update',
args = [{'visible': [True, True, True, True]},
{'title': 'All',
'showlegend':True}]),
dict(label = 'Decision Tree Prediction',
method = 'update',
args = [{'visible': [True, True, False, False]},
{'title': 'Linear Regression',
'showlegend':True}]),
dict(label = 'Linear Regression Prediction',
method = 'update',
args = [{'visible': [True, False, True, False]},
{'title': 'k-NN Regressor',
'showlegend':True}]),
dict(label = 'k-NN Regressor Prediction',
method = 'update',
args = [{'visible': [True, False, False, True]},
{'title': 'Decision Tree Regressor',
'showlegend':True}]),
]),
direction="down",
pad={"r": 10, "t": 10},
showactive=True,
x=0.1,
xanchor="left",
y=1.1,
yanchor="top"
),
]
)
fig.update_layout(title='Predicted Values for Facebook For the last 500 Days',
xaxis_title='Date',
yaxis_title='Close Price')
fig.update_layout(
autosize=False,
width=1000,
height=650,)
iplot(fig,show_link=False)
df = apple[['Close']].copy(deep=True)
future_days = 500
df['Prediction'] = df[['Close']].shift(-future_days)
X = np.array(df.drop(['Prediction'], 1))[:-future_days]
y = np.array(df['Prediction'])[:-future_days]
x_train, x_test, y_train, y_test = train_test_split(X, y, test_size = 0.25)
tree = DecisionTreeRegressor().fit(x_train, y_train)
lr = LinearRegression().fit(x_train, y_train)
knn = KNeighborsRegressor().fit(x_train, y_train)
x_future = df.drop(['Prediction'], 1)[:-future_days]
x_future = x_future.tail(future_days)
x_future = np.array(x_future)
tree_prediction = tree.predict(x_future)
lr_prediction = lr.predict(x_future)
knn_prediction = knn.predict(x_future)
predictions = tree_prediction
valid = df[X.shape[0]:].copy(deep=True)
valid['Predictions'] = predictions
valid[['Close','Predictions']]
fig = go.Figure()
fig.add_trace(go.Scatter(x=df.index.values, y=df['Close'], name='Actual Close',
line=dict(width=1.5)))
fig.add_trace(go.Scatter(x=valid.index.values, y=valid['Predictions'], name='Predicted Close D-Tree',
line=dict(width=1.5)))
predictions = lr_prediction
valid = df[X.shape[0]:].copy(deep=True)
valid['Predictions'] = predictions
valid[['Close','Predictions']]
fig.add_trace(go.Scatter(x=valid.index.values, y=valid['Predictions'], name='Predicted Close Lin Reg',
line=dict(width=1.5)))
predictions = knn_prediction
valid = df[X.shape[0]:].copy(deep=True)
valid['Predictions'] = predictions
valid[['Close','Predictions']]
fig.add_trace(go.Scatter(x=valid.index.values, y=valid['Predictions'], name='Predicted Close k-NN',
line=dict(width=1.5)))
fig.update_layout(
updatemenus=[
dict(
buttons=list([
dict(label = 'All',
method = 'update',
args = [{'visible': [True, True, True, True]},
{'title': 'All',
'showlegend':True}]),
dict(label = 'Decision Tree Prediction',
method = 'update',
args = [{'visible': [True, True, False, False]},
{'title': 'Linear Regression',
'showlegend':True}]),
dict(label = 'Linear Regression Prediction',
method = 'update',
args = [{'visible': [True, False, True, False]},
{'title': 'k-NN Regressor',
'showlegend':True}]),
dict(label = 'k-NN Regressor Prediction',
method = 'update',
args = [{'visible': [True, False, False, True]},
{'title': 'Decision Tree Regressor',
'showlegend':True}]),
]),
direction="down",
pad={"r": 10, "t": 10},
showactive=True,
x=0.1,
xanchor="left",
y=1.1,
yanchor="top"
),
]
)
fig.update_layout(title='Predicted Values for Apple For the last 500 Days',
xaxis_title='Date',
yaxis_title='Close Price')
fig.update_layout(
autosize=False,
width=1000,
height=650,)
iplot(fig,show_link=False)
df = amazon[['Close']].copy(deep=True)
future_days = 500
df['Prediction'] = df[['Close']].shift(-future_days)
X = np.array(df.drop(['Prediction'], 1))[:-future_days]
y = np.array(df['Prediction'])[:-future_days]
x_train, x_test, y_train, y_test = train_test_split(X, y, test_size = 0.25)
tree = DecisionTreeRegressor().fit(x_train, y_train)
lr = LinearRegression().fit(x_train, y_train)
knn = KNeighborsRegressor().fit(x_train, y_train)
x_future = df.drop(['Prediction'], 1)[:-future_days]
x_future = x_future.tail(future_days)
x_future = np.array(x_future)
tree_prediction = tree.predict(x_future)
lr_prediction = lr.predict(x_future)
knn_prediction = knn.predict(x_future)
predictions = tree_prediction
valid = df[X.shape[0]:].copy(deep=True)
valid['Predictions'] = predictions
valid[['Close','Predictions']]
fig = go.Figure()
fig.add_trace(go.Scatter(x=df.index.values, y=df['Close'], name='Actual Close',
line=dict(width=1.5)))
fig.add_trace(go.Scatter(x=valid.index.values, y=valid['Predictions'], name='Predicted Close D-Tree',
line=dict(width=1.5)))
predictions = lr_prediction
valid = df[X.shape[0]:].copy(deep=True)
valid['Predictions'] = predictions
valid[['Close','Predictions']]
fig.add_trace(go.Scatter(x=valid.index.values, y=valid['Predictions'], name='Predicted Close Lin Reg',
line=dict(width=1.5)))
predictions = knn_prediction
valid = df[X.shape[0]:].copy(deep=True)
valid['Predictions'] = predictions
valid[['Close','Predictions']]
fig.add_trace(go.Scatter(x=valid.index.values, y=valid['Predictions'], name='Predicted Close k-NN',
line=dict(width=1.5)))
fig.update_layout(
updatemenus=[
dict(
buttons=list([
dict(label = 'All',
method = 'update',
args = [{'visible': [True, True, True, True]},
{'title': 'All',
'showlegend':True}]),
dict(label = 'Decision Tree Prediction',
method = 'update',
args = [{'visible': [True, True, False, False]},
{'title': 'Linear Regression',
'showlegend':True}]),
dict(label = 'Linear Regression Prediction',
method = 'update',
args = [{'visible': [True, False, True, False]},
{'title': 'k-NN Regressor',
'showlegend':True}]),
dict(label = 'k-NN Regressor Prediction',
method = 'update',
args = [{'visible': [True, False, False, True]},
{'title': 'Decision Tree Regressor',
'showlegend':True}]),
]),
direction="down",
pad={"r": 10, "t": 10},
showactive=True,
x=0.1,
xanchor="left",
y=1.1,
yanchor="top"
),
]
)
fig.update_layout(title='Predicted Values for Amazon For the last 500 Days',
xaxis_title='Date',
yaxis_title='Close Price')
fig.update_layout(
autosize=False,
width=1000,
height=650,)
iplot(fig,show_link=False)
df = netflix[['Close']].copy(deep=True)
future_days = 500
df['Prediction'] = df[['Close']].shift(-future_days)
X = np.array(df.drop(['Prediction'], 1))[:-future_days]
y = np.array(df['Prediction'])[:-future_days]
x_train, x_test, y_train, y_test = train_test_split(X, y, test_size = 0.25)
tree = DecisionTreeRegressor().fit(x_train, y_train)
lr = LinearRegression().fit(x_train, y_train)
knn = KNeighborsRegressor().fit(x_train, y_train)
x_future = df.drop(['Prediction'], 1)[:-future_days]
x_future = x_future.tail(future_days)
x_future = np.array(x_future)
tree_prediction = tree.predict(x_future)
lr_prediction = lr.predict(x_future)
knn_prediction = knn.predict(x_future)
predictions = tree_prediction
valid = df[X.shape[0]:].copy(deep=True)
valid['Predictions'] = predictions
valid[['Close','Predictions']]
fig = go.Figure()
fig.add_trace(go.Scatter(x=df.index.values, y=df['Close'], name='Actual Close',
line=dict(width=1.5)))
fig.add_trace(go.Scatter(x=valid.index.values, y=valid['Predictions'], name='Predicted Close D-Tree',
line=dict(width=1.5)))
predictions = lr_prediction
valid = df[X.shape[0]:].copy(deep=True)
valid['Predictions'] = predictions
valid[['Close','Predictions']]
fig.add_trace(go.Scatter(x=valid.index.values, y=valid['Predictions'], name='Predicted Close Lin Reg',
line=dict(width=1.5)))
predictions = knn_prediction
valid = df[X.shape[0]:].copy(deep=True)
valid['Predictions'] = predictions
valid[['Close','Predictions']]
fig.add_trace(go.Scatter(x=valid.index.values, y=valid['Predictions'], name='Predicted Close k-NN',
line=dict(width=1.5)))
fig.update_layout(
updatemenus=[
dict(
buttons=list([
dict(label = 'All',
method = 'update',
args = [{'visible': [True, True, True, True]},
{'title': 'All',
'showlegend':True}]),
dict(label = 'Decision Tree Prediction',
method = 'update',
args = [{'visible': [True, True, False, False]},
{'title': 'Linear Regression',
'showlegend':True}]),
dict(label = 'Linear Regression Prediction',
method = 'update',
args = [{'visible': [True, False, True, False]},
{'title': 'k-NN Regressor',
'showlegend':True}]),
dict(label = 'k-NN Regressor Prediction',
method = 'update',
args = [{'visible': [True, False, False, True]},
{'title': 'Decision Tree Regressor',
'showlegend':True}]),
]),
direction="down",
pad={"r": 10, "t": 10},
showactive=True,
x=0.1,
xanchor="left",
y=1.1,
yanchor="top"
),
]
)
fig.update_layout(title='Predicted Values for Netflix For the last 500 Days',
xaxis_title='Date',
yaxis_title='Close Price')
fig.update_layout(
autosize=False,
width=1000,
height=650,)
iplot(fig,show_link=False)
df = google[['Close']].copy(deep=True)
future_days = 500
df['Prediction'] = df[['Close']].shift(-future_days)
X = np.array(df.drop(['Prediction'], 1))[:-future_days]
y = np.array(df['Prediction'])[:-future_days]
x_train, x_test, y_train, y_test = train_test_split(X, y, test_size = 0.25)
tree = DecisionTreeRegressor().fit(x_train, y_train)
lr = LinearRegression().fit(x_train, y_train)
knn = KNeighborsRegressor().fit(x_train, y_train)
x_future = df.drop(['Prediction'], 1)[:-future_days]
x_future = x_future.tail(future_days)
x_future = np.array(x_future)
tree_prediction = tree.predict(x_future)
lr_prediction = lr.predict(x_future)
knn_prediction = knn.predict(x_future)
predictions = tree_prediction
valid = df[X.shape[0]:].copy(deep=True)
valid['Predictions'] = predictions
valid[['Close','Predictions']]
fig = go.Figure()
fig.add_trace(go.Scatter(x=df.index.values, y=df['Close'], name='Actual Close',
line=dict(width=1.5)))
fig.add_trace(go.Scatter(x=valid.index.values, y=valid['Predictions'], name='Predicted Close D-Tree',
line=dict(width=1.5)))
predictions = lr_prediction
valid = df[X.shape[0]:].copy(deep=True)
valid['Predictions'] = predictions
valid[['Close','Predictions']]
fig.add_trace(go.Scatter(x=valid.index.values, y=valid['Predictions'], name='Predicted Close Lin Reg',
line=dict(width=1.5)))
predictions = knn_prediction
valid = df[X.shape[0]:].copy(deep=True)
valid['Predictions'] = predictions
valid[['Close','Predictions']]
fig.add_trace(go.Scatter(x=valid.index.values, y=valid['Predictions'], name='Predicted Close k-NN',
line=dict(width=1.5)))
fig.update_layout(
updatemenus=[
dict(
buttons=list([
dict(label = 'All',
method = 'update',
args = [{'visible': [True, True, True, True]},
{'title': 'All',
'showlegend':True}]),
dict(label = 'Decision Tree Prediction',
method = 'update',
args = [{'visible': [True, True, False, False]},
{'title': 'Linear Regression',
'showlegend':True}]),
dict(label = 'Linear Regression Prediction',
method = 'update',
args = [{'visible': [True, False, True, False]},
{'title': 'k-NN Regressor',
'showlegend':True}]),
dict(label = 'k-NN Regressor Prediction',
method = 'update',
args = [{'visible': [True, False, False, True]},
{'title': 'Decision Tree Regressor',
'showlegend':True}]),
]),
direction="down",
pad={"r": 10, "t": 10},
showactive=True,
x=0.1,
xanchor="left",
y=1.1,
yanchor="top"
),
]
)
fig.update_layout(title='Predicted Values for Google For the last 500 Days',
xaxis_title='Date',
yaxis_title='Close Price')
fig.update_layout(
autosize=False,
width=1000,
height=650,)
iplot(fig,show_link=False)